﻿using System;
using System.Security.Cryptography;
using System.Text;
namespace Yt.Core.Security
{
    /// <summary>
    /// AES对称加密解密算法
    /// </summary>
    public class AES
    {
        /// <summary>
        /// AES加密字符串
        /// </summary>
        /// <param name="sourceString">明文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <param name="outformat">输出字符串的格式</param>
        /// <returns>密文</returns>
        public static string AESEncryptString(string sourceString, string key, string iv, CipherMode Cmode = CipherMode.ECB, PaddingMode Pmode = PaddingMode.PKCS7)
        {
            byte[] inData = Encoding.UTF8.GetBytes(sourceString);
            byte[] temp = AESEncrypt(inData, key, iv, Cmode, Pmode);
            return Convert.ToBase64String(temp);
        }

        /// <summary>
        /// AES解密字符串
        /// </summary>
        /// <param name="encryptedString">密文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <param name="informat">输入的密文字符串的格式</param>
        /// <returns></returns>
        public static string AESDecryptString(string encryptedString, string key, string iv, CipherMode Cmode = CipherMode.ECB, PaddingMode Pmode = PaddingMode.PKCS7)
        {
            byte[] inData = Convert.FromBase64String(encryptedString);
            return Encoding.UTF8.GetString(AESDecrypt(inData, key, iv, Cmode, Pmode));
        }


        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="sourceString">明文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <returns>密文</returns>
        public static byte[] AESEncrypt(byte[] source, string key, string iv, CipherMode Cmode = CipherMode.ECB, PaddingMode Pmode = PaddingMode.PKCS7)
        {
            byte[] btKey = UTF8Encoding.UTF8.GetBytes(MD5.GetMD5(key));
            byte[] btIV = UTF8Encoding.UTF8.GetBytes(MD5.GetMD5(iv));
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = btKey;
            rDel.BlockSize = btIV.Length * 8;
            rDel.Mode = Cmode;
            rDel.Padding = Pmode;
            rDel.IV = btIV;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(source, 0, source.Length);
            rDel.Clear();
            cTransform.Dispose();
            rDel.Dispose();
            btKey = btIV = null;
            return resultArray;
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="encryptedString">密文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <returns>明文</returns>
        public static byte[] AESDecrypt(byte[] encrypted, string key, string iv, CipherMode Cmode = CipherMode.ECB, PaddingMode Pmode = PaddingMode.PKCS7)
        {
            byte[] btKey = UTF8Encoding.UTF8.GetBytes(MD5.GetMD5(key));
            byte[] btIV = UTF8Encoding.UTF8.GetBytes(MD5.GetMD5(iv));
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = btKey;
            rDel.BlockSize = btIV.Length * 8;
            rDel.Mode = Cmode;
            rDel.Padding = Pmode;
            rDel.IV = btIV;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(encrypted, 0, encrypted.Length);
            rDel.Clear();
            cTransform.Dispose();
            rDel.Dispose();
            btKey = btIV = null;
            return resultArray;
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="encryptedString">密文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <returns>明文</returns>
        public static byte[] AESDecrypt(byte[] encrypted, byte[] btKey, byte[] btIV, CipherMode Cmode = CipherMode.ECB, PaddingMode Pmode = PaddingMode.PKCS7)
        {
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = btKey;
            rDel.BlockSize = btIV.Length * 8;
            rDel.Mode = Cmode;
            rDel.Padding = Pmode;
            rDel.IV = btIV;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(encrypted, 0, encrypted.Length);
            rDel.Clear();
            cTransform.Dispose();
            rDel.Dispose();
            return resultArray;
        }

    }
}
